<template>
	<view class="pageBox">
		<!-- 标题栏 -->
		<head-navbar :back="true">
			<view>地址管理</view>
			<view slot="right" class="rightText" @click="toPage(0,'/pages/my/myService/setting/setAddress/setAddress?type=add')" data-type="0">新增</view>
		</head-navbar>
		<view class="content">
			<view class="list">
				<view class="item" v-for="(item,index) in addressList" :key="item.id">
					<view class="top" >
						<view class="left" @click="checkAddress(item)">
							<view class="title">
								<text class="name">{{ item.consigneeName }}</text>
								<text class="tel">{{ item.consigneePhone }}</text>
							</view>
							<view class="addres">
								{{item.region}}{{item.detailAddress}}
							</view>
						</view>
						<view class="right">
							<view @click="goToEdit(item)">
								<image src="../../../../../static/img/xiugai1.png" mode="aspectFit" style="width: 40upx;height: 40upx;"></image>
							</view>
							<view style="margin-top: 30upx;" @click="deleteAddress(item)">
								<image src="../../../../../static/img/shanchu1.png" mode="aspectFit" style="width: 40upx;height: 40upx;"></image>
							</view>

						</view>
					</view>
					<view class="bottom" @click="switchBtn(index)" v-if="item.defaultAddress == 1">
						<image src='../../../../../static/img/gou1.png' mode="aspectFit" style="width: 22upx;height: 22upx;"></image>
						<view class="text yes">默认地址</view>
					</view>
				</view>
			</view>
		
			<!-- <button type="default" class="summitBtn" @click="toPage(2)" v-if="addressList.length != 0">确定</button> -->
			<u-modal v-model="model_show" :content="content" :show-cancel-button="true" @cancel="cancelBtn" @confirm="confimBtn"></u-modal>
			<u-toast ref="uToast" />
		</view>
		
		<u-empty text="没有收货地址哦" mode="address" :show="addressList.length == 0" margin-top="120"></u-empty>
	</view>
</template>

<script>
import * as api from '@/api/user/address.js';
	export default {
		data() {
			return {
				model_show:false,
				content:'是否确定删除该地址？',
				type:'sset',
				addressList:[],
				goodsType:'',
				ids:'',
				orderType:''
			}
		},
		onLoad(option){
			this.type = option.type
			this.goodsType = option.goodsType
			this.ids2 = option.ids2
			this.orderType = option.orderType
		},
		methods: {
			// 获取列表
			async getList() {
				let param = {
					pageNum:1,
					pageSize:20
				}
				try{
					const ret = await api.getUserAddressList(param);
					this.addressList = ret.data.obj
				}catch(error){
					console.log(error)
				}
			},
			goToEdit(item) {
				uni.setStorage({
				    key: 'addressDetail',
				    data: item,
				    success: function () {
				        uni.navigateTo({
				        	url:'/pages/my/myService/setting/setAddress/setAddress'
				        })
				    }
				});
			},
			confimBtn() {
				this.$refs.uToast.show({
					title: '删除成功',
					type: 'success'
				})
			},
			cancelBtn() {
				console.log('取消删除地址')
			},
			async deleteAddress(item) {
				let param = {
					userAddressId:item.id
				}
				try{
					const ret = await api.deleteUserAddress(param);
					console.log(ret)
					this.utils.toast(ret.data.msg,'success');
					this.getList();
				}catch(error){
					console.log(error)
				}
			},
			openHeaderMenu() {
				
			},
			checkAddress(item){
				if(this.type == 'create') {
					this.$api.prePage().addressData = item;
					this.$api.prePage().noAddress = false;	
					if(this.orderType == 2){
						if(this.ids2!= ''){
							this.$api.prePage().getFreightCart(item.id)
						}
					}else{
						if(this.goodsType == 2){
							this.$api.prePage().getFreight2(item.id)
						}
					}
					uni.navigateBack();
				}else if(this.type == 'mysale'){
					this.$api.prePage().addressData = item;
					this.$api.prePage().noAddress = false;
					this.$api.prePage().addRessShow = true;
					uni.navigateBack();
				}
				
			},
			switchBtn(i) {
				this.addressList.forEach(e=>{
					e.defaultAddress = 0;
				})
				this.addressList[i].defaultAddress = 1; 
			}
		},
		onShow() {
			this.getList();
		}
	}
</script>

<style lang="scss">
@import  './index.scss';
</style>
